'use strict';

module.exports = app => {
    return class LikeService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 取消点赞
         * @param id
         * @returns {Promise<void>}
         */
        async cancel({ id }) {
            let url = this.app.urls('likeCancel');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    likeId: id
                }
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 点赞
         * type： 1.帖子 2.评论 3.回评点赞
         * @param id
         * @param type
         * @returns {Promise<void>}
         */
        async add({ id, targetId, type, role }) {
            let url = this.app.urls('likeAdd');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    domainId: id,
                    // objectId: id,
                    type,
                    roleType: role
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }




    }

};
